package com.chonglie;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

import com.chonglie.listview.Item;
import com.chonglie.listview.ItemViewHandler;
import com.chonglie.listview.ListViewAdapter;

public class ListViewActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.list_view);
		fillContent();
	}

	private void fillContent() {
		ListView lv = (ListView) findViewById(R.id.listview_01);
		List<Map<String, Object>> records = new ArrayList<Map<String, Object>>();
		Map<String, Object> map;
		for (int i = 0; i < 20; i++) {
			map = new HashMap<String, Object>();
			map.put("image", R.drawable.ic_launcher);
			map.put("title", "Title " + i);
			map.put("content", "Content " + i);
			records.add(map);
		}

		SimpleAdapter sa = new SimpleAdapter(this, records,
				R.layout.list_view_item, new String[] { "image", "title",
						"content" }, new int[] { R.id.listview_item_image,
						R.id.listview_item_title, R.id.listview_item_content });
		lv.setAdapter(sa);
	}

	private void fillContent2() {
		ListView lv = (ListView) findViewById(R.id.listview_01);
		List<Item> items = new ArrayList<Item>();
		for (int i = 0; i < 10; i++) {
			items.add(new Item("Title " + i, "Content " + i,
					R.drawable.ic_launcher));
		}

		ItemViewHandler handler = new ItemViewHandler(
				(LayoutInflater) this
						.getSystemService(Context.LAYOUT_INFLATER_SERVICE));
		ListViewAdapter lva = new ListViewAdapter(items, handler);
		lv.setAdapter(lva);
	}

	public void setTitle(View view) {
		TextView textView = (TextView) view
				.findViewById(R.id.listview_item_title);
		setTitle(textView.getText());
	}
}
